package com.zuo.mall.controller;

import cn.hutool.core.util.StrUtil;
import com.zuo.mall.common.api.CommonPage;
import com.zuo.mall.common.api.CommonResult;
import com.zuo.mall.dto.OmsOrderDeliveryParam;
import com.zuo.mall.dto.OmsOrderDetail;
import com.zuo.mall.dto.OmsOrderQueryParam;
import com.zuo.mall.dto.OmsReceiverInfoParam;
import com.zuo.mall.entity.OmsOrder;
import com.zuo.mall.entity.OmsOrderItem;
import com.zuo.mall.service.OmsOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Api("订单管理")
@RequestMapping("order")
public class OmsOrderController {
    @Autowired
    private OmsOrderService orderService;

    @GetMapping("list")
    public CommonResult<CommonPage<OmsOrder>> getList(OmsOrderQueryParam orderQueryParam,
                                                      @RequestParam(value = "pageNum", defaultValue = "10") Integer pageNum,
                                                      @RequestParam(value = "pageSize", defaultValue = "1") Integer pageSize
    ) {
        CommonPage<OmsOrder> omsOrderCommonPage = orderService.getPage(orderQueryParam, pageNum, pageSize);

        return CommonResult.success(omsOrderCommonPage);
    }


    @GetMapping("{id}")
    public CommonResult<OmsOrderDetail> getOrderDetail(@PathVariable("id") Long id) {
        OmsOrderDetail omsOrderDetail = orderService.getOrderDetailById(id);
        return CommonResult.success(omsOrderDetail);
    }

    @ApiOperation("修改收货人信息")
    @PostMapping("update/receiverInfo")
    public CommonResult updateReceiverInfo(@RequestBody OmsReceiverInfoParam receiverInfoParam) {
        int count = orderService.updateReceiverInfo(receiverInfoParam);
        if (count > 0) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();

    }


    @ApiOperation("修改订单备注")
    @PostMapping("update/note")
    public CommonResult updateNote(Long id, String note, Integer status) {
        int count = orderService.updateNoteById(id, note, status);
        if (count > 0) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }

    @ApiOperation("批量关闭订单")
    @PostMapping("/update/close")
    public CommonResult updateClose(@RequestParam List<Long> ids, @RequestParam String note) {
        int count = orderService.closeOrderBatch(ids, note);
        if (count>0) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }


    @ApiOperation("批量逻辑删除订单")
    @PostMapping("delete")
    public CommonResult deleteOrderBatch(@RequestParam("ids") List<Long> ids) {
        int count = orderService.deleteOrderBatch(ids);
        if (count> 0) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }

    @ApiOperation("批量发货")
    @PostMapping("update/delivery")
    public CommonResult updateDeliver(@RequestBody List<OmsOrderDeliveryParam> deliveryParams) {
        int count = orderService.deliveryBatch(deliveryParams);
        if (count> 0) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }

}
